//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    welcome: '欢迎进入炮弹游戏互动平台',
    userInfo: {},
    hasUserInfo: false,
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false, 
    datas: [],  
    // canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../dh/dh',
    })
  },

  bindViewTap2: function () {
    wx.navigateTo({
      url: '../about/about',
    })
  },

  bindViewTap3: function () {
    var userName = app.globalData.userInfo.nickName
    var userHead = app.globalData.userInfo.avatarUrl
    var openid = this.data.datas;
    wx.navigateTo({
      url: '../history/history?username=' + userName + '&openid=' + openid + '&userHead=' + userHead,
    })
  },

  onLoad: function (options) {
    var self = this;
    var myCode;
    var src = decodeURIComponent(options.q)
    console.log(src)

    if (src == "undefined"){
      wx.login({
        success: res => {
          console.log(res.code)
          myCode = res.code
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo
              this.setData({
                userInfo: res.userInfo,
                hasUserInfo: true
              })
              console.log(res.userInfo)
              wx.request({
                url: 'https://www.zijike.com/sapplogin',
                data: {
                  code: myCode,
                  url: res.userInfo.avatarUrl,
                  name: res.userInfo.nickName,
                  province: res.userInfo.province,
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  var openId = res.data.openId
                  console.log(openId)
                  self.setData({
                    datas: openId
                  })
                  wx.showModal({
                    title: '欢迎进入炮弹游戏',
                    content: '可以开始游戏',
                    success: (res)=> {
                      if (res.confirm) {
                        console.log('确认')
                      }else {
                        console.log('取消')
                      }
                    }
                  })
                  // wx.showToast({
                  //   title: '游戏启动成功',
                  //   icon: 'success',
                  //   duration: 2000,
                  //   success: (res)=> {
                  //     console.log(res)
                  //   }
                  // })
                }
              })
            }
          })
        }
      })
    }else{
      var id = src.split('=')

      var uid = id[1].split('&')[0]
      var did = id[2].split('&')[0]
      var nameId = id[3]

      console.log(uid)
      console.log(did)
      console.log(nameId)
      console.log("1111")
      console.log(id)
      console.log("11111")

      wx.login({
        success: res => {
          console.log(res.code)
          myCode = res.code
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo
              this.setData({
                userInfo: res.userInfo,
                hasUserInfo: true
              })
              console.log(res.userInfo)
              wx.request({
                url: 'https://www.zijike.com/sapplogin',
                data: {
                  code: myCode,
                  url: res.userInfo.avatarUrl,
                  name: res.userInfo.nickName,
                  province: res.userInfo.province,
                  gameid: uid,
                  deviceid: did,
                  nameId: nameId
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  var openId = res.data.openId
                  console.log(openId)
                  self.setData({
                    datas: openId
                  })
                }
              })
            }
          })
        }
      })
    }
    
    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    
    
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
